home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / hmac.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  4KB  |  125 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''HMAC (Keyed-Hashing for Message Authentication) Python module.
  5.  
  6. Implements the HMAC algorithm as described by RFC 2104.
  7. '''
  8. import warnings as _warnings
  9. trans_5C = []([ chr(x ^ 92) for x in xrange(256) ])
  10. trans_36 = []([ chr(x ^ 54) for x in xrange(256) ])
  11. digest_size = None
  12. _secret_backdoor_key = []
  13.  
  14. class HMAC:
  15.     '''RFC 2104 HMAC class.  Also complies with RFC 4231.
  16.  
  17.     This supports the API for Cryptographic Hash Functions (PEP 247).
  18.     '''
  19.     blocksize = 64
  20.     
  21.     def __init__(self, key, msg = None, digestmod = None):
  22.         '''Create a new HMAC object.
  23.  
  24.         key:       key for the keyed hash object.
  25.         msg:       Initial input for the hash, if provided.
  26.         digestmod: A module supporting PEP 247.  *OR*
  27.                    A hashlib constructor returning a new hash object.
  28.                    Defaults to hashlib.md5.
  29.         '''
  30.         if key is _secret_backdoor_key:
  31.             return None
  32.         if digestmod is None:
  33.             import hashlib as hashlib
  34.             digestmod = hashlib.md5
  35.         
  36.         if hasattr(digestmod, '__call__'):
  37.             self.digest_cons = digestmod
  38.         else:
  39.             
  40.             self.digest_cons = lambda d = ('',): digestmod.new(d)
  41.         self.outer = self.digest_cons()
  42.         self.inner = self.digest_cons()
  43.         self.digest_size = self.inner.digest_size
  44.         if hasattr(self.inner, 'block_size'):
  45.             blocksize = self.inner.block_size
  46.             if blocksize < 16:
  47.                 _warnings.warn('block_size of %d seems too small; using our default of %d.' % (blocksize, self.blocksize), RuntimeWarning, 2)
  48.                 blocksize = self.blocksize
  49.             
  50.         else:
  51.             _warnings.warn('No block_size attribute on given digest object; Assuming %d.' % self.blocksize, RuntimeWarning, 2)
  52.             blocksize = self.blocksize
  53.         if len(key) > blocksize:
  54.             key = self.digest_cons(key).digest()
  55.         
  56.         key = key + chr(0) * (blocksize - len(key))
  57.         self.outer.update(key.translate(trans_5C))
  58.         self.inner.update(key.translate(trans_36))
  59.         if msg is not None:
  60.             self.update(msg)
  61.         
  62.  
  63.     
  64.     def update(self, msg):
  65.         '''Update this hashing object with the string msg.
  66.         '''
  67.         self.inner.update(msg)
  68.  
  69.     
  70.     def copy(self):
  71.         """Return a separate copy of this hashing object.
  72.  
  73.         An update to this copy won't affect the original object.
  74.         """
  75.         other = self.__class__(_secret_backdoor_key)
  76.         other.digest_cons = self.digest_cons
  77.         other.digest_size = self.digest_size
  78.         other.inner = self.inner.copy()
  79.         other.outer = self.outer.copy()
  80.         return other
  81.  
  82.     
  83.     def _current(self):
  84.         '''Return a hash object for the current state.
  85.  
  86.         To be used only internally with digest() and hexdigest().
  87.         '''
  88.         h = self.outer.copy()
  89.         h.update(self.inner.digest())
  90.         return h
  91.  
  92.     
  93.     def digest(self):
  94.         '''Return the hash value of this hashing object.
  95.  
  96.         This returns a string containing 8-bit data.  The object is
  97.         not altered in any way by this function; you can continue
  98.         updating the object after calling this function.
  99.         '''
  100.         h = self._current()
  101.         return h.digest()
  102.  
  103.     
  104.     def hexdigest(self):
  105.         '''Like digest(), but returns a string of hexadecimal digits instead.
  106.         '''
  107.         h = self._current()
  108.         return h.hexdigest()
  109.  
  110.  
  111.  
  112. def new(key, msg = None, digestmod = None):
  113.     """Create a new hashing object and return it.
  114.  
  115.     key: The starting key for the hash.
  116.     msg: if available, will immediately be hashed into the object's starting
  117.     state.
  118.  
  119.     You can now feed arbitrary strings into the object using its update()
  120.     method, and can ask for the hash value at any time by calling its digest()
  121.     method.
  122.     """
  123.     return HMAC(key, msg, digestmod)
  124.  
  125.